<template>
	<view class="info">
		<view class="banner">
			<swiper class="swiper" indicator-dots="true" style="height: 634rpx; width: 100%;">
				<swiper-item v-for="item in lifoLinst.skuList ">
					<image :src="item.pic" style="width: 100%; height: 100%;"></image>
					<!-- <image :src="item.albumPics"></image> -->
				</swiper-item>

			</swiper>
			<uni-icons type="star" size="30" :color="isClled ? 'red' : ''" @tap="collect">

			</uni-icons>
		</view>

		<view class="title-block">
			<view class="boxs">
				<view style="color: #f2270c;">¥</view>
				<view class="price ">

					<view class="money"> {{lifoLinst.promotionPrice}}</view>
				</view>

				<view class="disabled">¥{{lifoLinst.productSn}}.00</view>


				<view class="promotion-type">没有促销使用原价</view>
			</view>
			<view class="detail_title">{{lifoLinst.name}}</view>
			<view class="detail_desc">{{lifoLinst.description}}</view>

		</view>


		<view class="content">
			<!-- <view v-html="lifoLinst.detailMobileHtml"></view> -->
			<!-- 插件mp-html可以更改里面的样式-->
			<mp-html :tag-style="obj" :content="lifoLinst.detailMobileHtml" />
		</view>
		<!-- 弹窗  @touchmove滑动事件-->
		<view class="mask" @touchmove.stop.prevent="" v-if="isMaskShow">
			<view class="box">
				<uni-icons class="card" type="closeempty" size="21" @tap="touch"></uni-icons>
				<image class="img" :src="imgg"></image>
				<view class="title">{{lifoLinst.name}}</view>
				<view class="sku-count">库存数量</view>
				<view class="colors-area">
					<view class="common-title">商品选择</view>

					<view class="colors">
						<view class="colorsq" v-for="(item,index) in skuList" :key="index">
							<view :class="bokk == index ? 'color-item active' : 'color-item' " @click="colo(index)">
								{{sku(item.spData)}}
							</view>
						</view>
					</view>
						
						<view class="common-title">特色服务</view>
						
						<view class="colors">
							<view class="colorsq " >
								<view class="color-item">
									送货上门
								</view>
								<view class="color-item">
									送货上门
								</view>
							</view>
						</view>
				</view>
		
				<view class="add">
					购买数量
					<uni-number-box v-model="vModelValue" @change="tttttt"/>
				</view>
				
				<button style="width: 40%; margin: 0 auto; margin-top: 40rpx; margin-bottom: 20rpx;" @click="save">确定</button>

			</view>
		</view>

		<view class="goods_nav">
			<!-- 底部 -->
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import {
		productDetail,
		addCart,
		addPreOrder
	} from "@/api/kind/info/index.js"
	import uniGoodsNav from "@/components/uni-goods-nav/uni-goods-nav.vue"
	import mix from "@/mixins/index.js"
	export default {
		components: {
			uniGoodsNav
		},
		mixins:[mix],  //this.checkLogin()方法
		data() {
			return {
				skuid:"", //弹窗里的id
				currenTab:"",//存的是点击tab栏传入的0 和 1
				vModelValue:1,
				bokk:0,
				isMaskShow: false,
				options: [{
						icon: 'headphones',
						text: '客服'
					}, 
					{
						icon: 'cart',
						text: '购物车',
						// info: 2
					}
				],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				],
				isClled: false,
				lifoLinst: {}, //获取过来的数据
				skuList: [], // 弹窗数据
				imgg:0,
				obj: {
					img: "width:80%; height: auto; display: block;margin: 0 auto; ",
				}
			};
		},
		
		onLoad(id) {
			var id = id.id;
			// console.log("我是上个页面传递过来的",id);
			this.init(id);
			// 判断是否收藏
			var collectedList = uni.getStorageSync("collected");
			// 第一种情况本地没有对应的数据
			if (!collectedList) {
				this.isClled = false;
			} else {
				// 本地有数据没有对应的id
				var item = collectedList.find(ele => ele == id);
				if (item) {
					// 本地有id就是收藏过了
					this.isClled = true;
				} else {
					// 本地没有id
					this.isClled = false;
				}
			}
		

		},
		
		methods: {
			//根据index来添加边框颜色
			colo(index){
				
			// console.log("我是商品分类",index,id);
			// this.skuid = id; //获取弹窗里id 
			this.bokk= index;
			// this.imgg = index;
			this.imgg =  this.skuList[index].pic
			console.log("获取过来的id",this.lifoLinst.skuList);
			
			},
			//数组里面
			sku(vall) {
				var arr = JSON.parse(vall);
				var spd = "";
				arr.forEach(el => {
					spd += (el.key + ': ' + el.value + '  ');

				})
				return spd
			},
			// 点击收藏
			collect() {
				// 获取存到本地的collected
				var collectedList = uni.getStorageSync("collected")
				// 点击收藏吧当前商品存到本地
				if (!collectedList) {
					//如果本地没有就添加上
					uni.setStorageSync("collected", [this.lifoLinst.id])
					this.isClled = true;
				} else {
					// 如果有就删除
					var item = collectedList.find(ele => ele == this.lifoLinst.id);
					var index = collectedList.findIndex(ele => ele == this.lifoLinst.id);
					// 再次点击时候判断有无本地的数据 有的话就取消收藏
					if (item) {
						//根据下标删除
						collectedList.splice(index, 1)
						this.isClled = false;
					} else {
						//如果没有就添加进去
						collectedList.push(this.lifoLinst.id);
						this.isClled = true;
					}
					//修改之后的数据在替换掉
					uni.setStorageSync("collected", collectedList)
				}
			},
			init(id) {
				productDetail(id)
					.then(res => {
						console.log(res);
						this.lifoLinst = res.data.product;
						this.skuList = res.data.product.skuList;
						this.imgg =  res.data.product.skuList[0].pic;
						// console.log(this.imgg);
						uni.setNavigationBarTitle({ //动态设置导航栏标题
							title: res.data.product.name
						})
					})
			},
			// 点击左侧
			onClick(e) {
				console.log("点击左侧按钮", e);
			},
			// 点击购买 index有0 1 一个是加入购物车 一个是立即购买
			buttonClick(e) {
				console.log("点击购买了", e);
				this.isMaskShow = true; //控制弹窗显示隐藏
				this.currenTab = e.index;  //根据传入的0 .1 来进行下一步的判断
				// console.log(this.currenTab);

			},
			// 关闭弹窗
			touch() {
				this.isMaskShow = false; //控制弹窗显示隐藏
			},
			//点击弹窗里的确定
			save(){
				if(!this.checkLogin()){
					return
				}
				// 根据this.currenTab来判断点击的是购物车还是添加购物车
				if(this.currenTab){
					// 为1就是true 执行立即购买参数  //记得要判断有无登录
					addPreOrder({
						  "orderItemList": [
						    {
						      "cartId": "string",
						      "productId": this.lifoLinst.id,
						      "productQuantity": this.vModelValue,
						      "productSkuId": this.lifoLinst.skuList[this.bokk].id,
						    }
						  ],
					}).then(res=>{
						console.log("打印立即购买",res);
						if(!res.success){
							uni.showToast({
							    title:res.message,
								icon:"none",
							    duration: 2000
							});
							
						}else{
							uni.navigateTo({
								url:`../../../selectAddress/selectAddress?id=${res.data.orderId}`
							})
						}
						
					})
					
				}else{
					//为0就是false 执行添加购物车操作  //记得要判断有无登录
					addCart({
						"productId": this.lifoLinst.id,
						"productSkuId":this.lifoLinst.skuList[this.bokk].id,
						"quantity":this.vModelValue,
					}).then(res=>{
						if(res.success){
							console.log("打印加入购物车",res);
							uni.showToast({
							    title: '加入成功',
							    duration: 2000
							});
						}
						
					})
					
				}
			},
			// 数量里面的vael
			tttttt(e){
				// console.log(e);
				this.vModelValue=e
			}
		}


	}
</script>

<style lang="scss" scoped>
	.info {
		background-color: #F1ECE7;

		// 导航置底
		.goods_nav {

			position: fixed;
			bottom: 0;
			width: 100%;
		}

		// 弹窗
		.mask {
			width: 100%;
			height: 100%;
			position: fixed;
			left: 0;
			top: 0;
			z-index: 300;
			background-color: rgba(0, 0, 0, 0.6);
			display: flex;
			align-items: center;
			justify-content: center;

			.box {
				width: 628rpx;
				// height: 926rpx;
				// height: 80%;
				background-color: #FFFFFF;
				border-radius: 40rpx;
				position: fixed;

				.card {
					position: absolute;
					left: 80%;
					top: 40rpx;
				}

				.img {
					position: absolute;
					width: 192rpx;
					height: 192rpx;
					background-color: #007AFF;
					left: 62rpx;
					top: -56rpx;
					border-radius: 10rpx;
				}

				.title {
					font-size: 28rpx;
					color: #3e3e3e;
					margin-left: 284rpx;
					margin-top: 84rpx;
					font-weight: 600;
				}

				.sku-count {
					font-size: 24rpx;
					color: #b0b0b0;
					margin-top: 6rpx;
					line-height: 32rpx;
					margin-left: 284rpx;
				}
				.colors-area{
					
					.common-title{
						margin: 20rpx;
						font-size: 28rpx;
						color: #3e3e3e;
						line-height: 40rpx;
						letter-spacing: 4rpx;
						font-weight: 600;
					}
					//动态添加样式
					.active{
						border: 2px #354e44 solid;
					}
					.colors{
						display: flex;
						flex-direction: column;
						align-items: center;
						.colorsq{
							display: flex;
							flex-direction: column;
							.color-item{
								text-indent: 1em;
								width: 462rpx;
								border-radius: 20rpx;
								 margin: 10rpx;
								 line-height: 60rpx;
								 background: #f2f4f3;
							}
						}
						
					}
				}

			}
			.add{
				margin-top: 40rpx;
				display: flex;
				justify-content: space-around;
				align-items: center;
			}
		}


		.title-block {
			padding: 70rpx 68rpx;

			.boxs {
				display: flex;
				align-items: baseline;

				.price {
					color: #f2270c;


					.money {
						font-size: 60rpx;
						font-weight: 700;
					}
				}

				.disabled {
					text-decoration: line-through;
					margin: 0 8rpx 0 0;
					color: #b5b5b5;
					font-size: 12px;
					font-weight: 700;
				}

				.promotion-type {
					font-size: 24rpx;
					padding: 0 6px;
					border: 1px solid #ff8373;
					color: #fff;
					background-color: #f2270c;
				}
			}

			.detail_title {

				color: #262626;
				overflow: hidden;
				font-size: 16px;
				font-weight: 700;


			}

			.detail_desc {
				font-size: 12px;
				padding: 18px 0 0;
				line-height: 1.3;
				color: #666;

			}

		}
	}
</style>
